﻿using Sasa.QualityTools.CodeAnalysis.Core.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;

namespace Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.ViewModels.Configurations
{
    public class CodeAnalysisConfigurationViewModel : ViewModelBase
    {
        public event EventHandler Saving;

        public event EventHandler Saved;

        public event EventHandler CloseRequired;


        private CodeAnalysisConfiguration configuration;

        private MSBuildProject project;

        private bool isSupportedCodeAnalysis;



        public GenericConfigurationViewModel GenericConfiguration { get; set; }

        public BuildConfigurationViewModel BuildConfiguration { get; set; }

        public FilterConfigurationViewModel FilterConfiguration { get; set; }

        public bool EnableCodeAnalysis
        {
            get
            {
                return configuration.EnableCodeAnalysis;
            }
            set
            {
                configuration.EnableCodeAnalysis = value;
                OnPropertyChanged("EnableCodeAnalysis");
            }
        }

        public bool IsSupportedCodeAnalysis
        {
            get
            {
                return isSupportedCodeAnalysis;
            }
            set
            {
                isSupportedCodeAnalysis = value;
                OnPropertyChanged("IsSupportedCodeAnalysis");
            }
        }

        public RelayCommand SaveCommand { get; private set; }

        public RelayCommand CancelCommand { get; private set; }


        private CodeAnalysisConfigurationViewModel()
        {
            CancelCommand = new RelayCommand(o => Close());
        }

        public CodeAnalysisConfigurationViewModel(CodeAnalysisConfiguration configuration)
            : this()
        {
            this.configuration = configuration;
            
            GenericConfiguration = new GenericConfigurationViewModel(configuration);
            FilterConfiguration = new FilterConfigurationViewModel(configuration);
            
            SaveCommand = new RelayCommand(o => Save());
        }

        public CodeAnalysisConfigurationViewModel(CodeAnalysisConfiguration configuration, MSBuildProject project)
            : this(configuration)
        {
            this.project = project;
            BuildConfiguration = new BuildConfigurationViewModel(project);
        }


        public void Save()
        {
            OnSaving(EventArgs.Empty);

            configuration.FilterPatterns.Clear();
            foreach (var pattern in FilterConfiguration.FilterPatterns)
            {
                configuration.FilterPatterns.Add(pattern.Value);
            }
            configuration.Save();

            if (project != null)
            {
                project.Save();
            }

            OnSaved(EventArgs.Empty);
            Close();
        }

        public void Close()
        {
            OnCloseRequired(EventArgs.Empty);
        }

        protected virtual void OnSaving(EventArgs e)
        {
            if (Saving != null)
            {
                Saving(this, e);
            }
        }

        protected virtual void OnSaved(EventArgs e)
        {
            if (Saved != null)
            {
                Saved(this, e);
            }
        }

        protected virtual void OnCloseRequired(EventArgs e)
        {
            if (CloseRequired != null)
            {
                CloseRequired(this, e);
            }
        }
    }
}
